//urp
Shader "czs/water"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "black" {}
		_Color("Color",Color) = (1,1,1,1)
		_DepthMin("DepthMin",Range(0,1)) = 0.1
		_DepthMax("DepthMax",Range(0,1)) = 0.2
        _WaterColorShallow("WaterColorShallow",Color) = (0,0,0.5,1)
        _WaterColorDeep("WaterColorDeep",Color) = (0,0,1,1)
        
        [Toggle]_useFoam("useFoam",int) = 0 //0:useScreenUV,1:useRefractTextur
        _FoamShapeTex("FoamShapeTex",2D) = "white"{}
        _FoamRange("FoamRange",Range(0.01,3)) = 0.1
        _FoamSmoothness("FoamSmoothness",Range(0.01,3)) = 0.1

        [Toggle]_UseRefract("useRefract",int) = 0 //0:useScreenUV,1:useRefractTextur
        [NoScaleOffset]_RefractTex("RefractTex",2D) = "white"{}
        _RefractFactor("_RefractFactor",Range(0.01,0.1)) = 0.01
	}
	SubShader
	{
		Tags
        { 
            "RenderType"="Transparent"
            "RenderPipeline"="UniversalRenderPipeline"
        }
		LOD 100
		Pass
		{
			Name "Unlit"
            Tags
            {
                "LightMode"="UniversalForward"
            }

            Blend SrcAlpha OneMinusSrcAlpha  // 混合因子
           
		}
        HLSLPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
            half3 normalOS:NORMAL;
        };
        struct v2f
        {
            float2 uv : TEXCOORD0;
            float4 vertex : SV_POSITION;
            half3 normalWS:TEXCOORD1;
            half3 posWS:TEXCOORD2;
        };

        sampler2D _CameraDepthTexture;
        sampler2D _CameraOpaqueTexture;//camera打勾

        sampler2D _MainTex;
        float4 _MainTex_ST;
        half4 _MainColor;
        float4 _Color;

        float _DepthMin;
        float _DepthMax;
        half4 _WaterColorShallow;
        half4 _WaterColorDeep;

        float _useFoam;
        sampler2D _FoamShapeTex;  float4 _FoamShapeTex_ST;//tilling and offset
        float _FoamRange;//tilling and offset
        float _FoamSmoothness;//tilling and offset

        float _UseRefract;
        float _RefractFactor;
        sampler2D _RefractTex; 
        // float4 _RefractTex_ST;
   
   
        
//aboveWater use ScreenUV , underWater use Lerp SUV& RefractTex
    
        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = TransformObjectToHClip(v.vertex.xyz);
            o.uv = TRANSFORM_TEX(v.uv, _MainTex);
            o.normalWS = TransformObjectToWorldNormal(v.normalOS);
            o.posWS = TransformObjectToWorld(v.vertex);
            o.uv = TRANSFORM_TEX(v.uv, _FoamShapeTex)+_Time.x;// 将uv坐标转换为纹理坐标
            return o;
        }
        half4 frag (v2f i) : SV_Target
        {
        
    float2 uvScreenOrig = i.posCS.xy / _ScreenParams.xy;  // 屏幕空间的uv坐标    
    
    

   float depthOrig = tex2D(_CameraDepthTexture, uvScreenOrig).r;  // 从深度纹理中采样深度值
   
    depthOrig = LinearEyeDepth(depthOrig,_ZBufferParams);  // 线性化深度值
        //depthRefract *= _DepthMax; //d3d
        //水的深度 = 场景中不透明物体的深度 - 水面各个片元在观察空间的z值
        //i.posVS.z:Unity观察空间用右手坐标系，z是负数，离摄像机越近，越接近于0
        float heightOrig =depthOrig + i.posVS.z;//水底到水面的高度 

//=======水面屏幕uv，水下扭曲uv=========================================
        float5 colorRefract = 0 ;
        //    return tex2D(_FoamShapeTex,uvScreenRefract);//debugger
        if(_UseRefract){
             float2 uvScreenRefract = uvScreenOrig;  // 原始的屏幕空间uv坐标,先把不扰动save
            //refract=================
            float2 uvRefract = tex2D(_RefractTex, i.uv).rg;  // 从折射纹理中采样uv坐标
            
            float depthRefract = tex2D(_CameraDepthTexture, uvScreenRefract).r;  // 从深度纹理中采样深度值
            depthRefract = LinearEyeDepth(depthRefract,_ZBufferParams);  // 线性化深度值
            float heightRefract =depthRefract + i.posVS.z;//水底到水面的高度 

            
            //扭曲
            uvScreenRefract= lerp(uvScreenRefract, uvRefract, _RefractFactor);  // 根据折射因子进行lerp操作
            //refract=================
            //overWater No Refract
            if(heightRefract<0){
            uvScreenRefract = uvScreenOrig;
            }
            //画一块板，板上画underSea的thing
            colRefract = tex2D(_CameraOpaqueTexture, uvScreenRefract);
            // return colRefract;//debugger
        }
//================================================
        float foamRange =0.0;
        half foamShape =0.0;
        if(_useFoam){
            //NOTE:没有水底的水面，height和foamRange 无穷大Infinity
            foamRange = _FoamRange*heightOrig;//不应该用深度缓冲扰动过的水面高度
           foamShape = tex2D(_FoamShapeTex,uvScreenRefract).r;

        // _FoamSmoothness less than 1,use pow for more smooth
        foamShape = pow(foamShape,_FoamSmoothness);// 抹平差异
        foamShape =  step(foamRange,foamShape );
        // return foamShape;//debugger
        }

        

        //对纹理采样
        half4 colWater = lerp(_WaterColorShallow,_WaterColorDeep,foamRange);
        colWater.a = 0.5 ;//注释掉

        //做transmission refract，make water underWater
        // 空气扭曲 dissolve
        // normalWaterTex
        // foam howtodo texture
            // return colWater;
            return foamShape + colWater*colRefract;
        }
        ENDHLSL
    
    
    }
}